﻿/**车辆借调信息**/

//借出基地change事件
function BaseChange(obj) {
    debugger
    var selectSource = new Object();
    selectSource.FieldEnumDataIns = "CPYKWL";
    selectSource.FieldEnumDataSource = "select CarNumber [text] ,Id  value from BD_CarInfo where  BaseId='{0}' order by Id";
    //绑定基地信息
    var url = "/api/HatchRoom/GetSourceInfo";
    var CPForm_11870 = $("#CPForm_11870");
    if (CPForm_11870.val() == "") return;
    var select = new Object();
    select.FieldEnumDataIns = selectSource.FieldEnumDataIns;
    select.FieldEnumDataSource = selectSource.FieldEnumDataSource.format(CPForm_11870.val());
    $.ajax({
        type: "POST",
        url: url,
        data: JSON.stringify(select),
        contentType: 'application/json',
        success: function (data) {
            if (data.length !== 0) {
                debugger;
                $("#CPForm_11868").find("option").remove(); 
                //json字符串转化为json对象
                data = eval('(' + data + ')');
                data = eval('(' + data.Column + ')');
                $("#CPForm_11868").append("<option value ='0'>请选择车牌号</option>");
                for (var i = 0; i < data.length; i++) {
                    var columnObj = data[i];
                    var html= "<option value ='" + columnObj.valueEx + "'>" + columnObj.textEx + "</option>";
                    $("#CPForm_11868").append(html);
                }
            }
        }
    });
    SetCarDriver();
}

//设置主副司机数据源
function SetCarDriver() {
    var selectSource = new Object();
    selectSource.FieldEnumDataIns = "CPOrganIns";
    selectSource.FieldEnumDataSource = "select  LoginName+'-'+UserName  as text, a.UserId as value from dbo.CP_User a inner join dbo.CP_RoleUser b on  a.UserId=b.UserId ";
    selectSource.FieldEnumDataSource += "INNER JOIN dbo.CP_DepUser d on a.UserId = d.UserId where b.RoleId = 1013  AND DepId in (select DepId from dbo.CP_Dep where ParentId in ({0}) and DepType = 5) ";


    //绑定基地信息
    var url = "/api/HatchRoom/GetSourceInfo";

    var CPForm_11870 = $("#CPForm_11870");//借出基地
    var CPForm_11872 = $("#CPForm_11872");//借入基地
    var values = "";
    if (CPForm_11870.val() !== "") values = CPForm_11870.val() + ",";
    if (CPForm_11872.val() !== "") values += CPForm_11872.val();
    values = (values.substring(values.length - 1) == ',') ? values.substring(0, values.length - 1) : values;
    var select = new Object();
    select.FieldEnumDataIns = selectSource.FieldEnumDataIns;
    select.FieldEnumDataSource = selectSource.FieldEnumDataSource.format(values);
    $.ajax({
        type: "POST",
        url: url,
        data: JSON.stringify(select),
        contentType: 'application/json',
        success: function (data) {
            if (data.length !== 0) {
                debugger;
                $("#CPForm_13181").find("option").remove(); 
                $("#CPForm_13183").find("option").remove(); 
                //json字符串转化为json对象
                data = eval('(' + data + ')');
                data = eval('(' + data.Column + ')');
                $("#CPForm_13181").append("<option value ='0'>请选择主司机</option>");
                $("#CPForm_13183").append("<option value ='0'>请选择辅司机</option>");
                for (var i = 0; i < data.length; i++) {
                    var columnObj = data[i];
                    var html = "<option value ='" + columnObj.valueEx + "'>" + columnObj.textEx + "</option>";
                    $("#CPForm_13181").append(html);
                    $("#CPForm_13183").append(html);
                }
            }
        }
    });
}

//选择车辆默认主辅司机
function SetDefaultDriver() {
    debugger
    var selectSource = new Object();
    selectSource.FieldEnumDataIns = "CPOrganIns";
    selectSource.FieldEnumDataSource = "select  LoginName+'-'+UserName  as text, UserId as value from dbo.CP_User where UserId in (select MainDriverId from  YKWL.dbo.BD_CarInfo where Id='{0}') ";

    var CPForm_11868 = $("#CPForm_11868");
    var content = CPFormGlobal_FormObj.Data.BD_CarBorrowInfo_CarId == "" ? $("#CPForm_11868").val() : CPFormGlobal_FormObj.Data.BD_CarBorrowInfo_CarId;
    //绑定基地信息
    var url = "/api/HatchRoom/GetSourceInfo";
    var select = new Object();
    select.FieldEnumDataIns = selectSource.FieldEnumDataIns;
    select.FieldEnumDataSource = selectSource.FieldEnumDataSource.format(content);
    $.ajax({
        type: "POST",
        url: url,
        data: JSON.stringify(select),
        contentType: 'application/json',
        success: function (data) {
            if (data.length !== 0) {
                debugger;
                //json字符串转化为json对象
                data = eval('(' + data + ')');
                data = eval('(' + data.Column + ')');
                var CPForm_13181 = $("#CPForm_13181");
                if (CPForm_13181.val() == 0 && data.length > 0) {
                    var columnObj = data[0];
                    $("#CPForm_13181 option").each(function () {
                        if ($(this).val() == columnObj.valueEx) {
                            $("#CPForm_13181 option[value='" + $(this).val() + "']").attr("selected", "selected");
                            return false;
                        }
                    });
                }

            }
        }
    });



    var AssistantDriverSource = new Object();
    AssistantDriverSource.FieldEnumDataIns = "CPOrganIns";
    AssistantDriverSource.FieldEnumDataSource = "select  LoginName+'-'+UserName  as text, UserId as value from dbo.CP_User where UserId in (select AssistantDriverId from  YKWL.dbo.BD_CarInfo where Id='{0}') ";

    //绑定基地信息
    var DriverSource = new Object();
    DriverSource.FieldEnumDataIns = AssistantDriverSource.FieldEnumDataIns;
    DriverSource.FieldEnumDataSource = AssistantDriverSource.FieldEnumDataSource.format(content);
    $.ajax({
        type: "POST",
        url: url,
        data: JSON.stringify(DriverSource),
        contentType: 'application/json',
        success: function (data1) {
            if (data1.length !== 0) {
                debugger;
                //json字符串转化为json对象
                data1 = eval('(' + data1 + ')');
                data1 = eval('(' + data1.Column + ')');
                var CPForm_13181 = $("#CPForm_13183");
                if (CPForm_13181.val() == 0 && data1.length > 0) {
                    var columnObj1 = data1[0];
                    $("#CPForm_13183 option").each(function () {
                        if ($(this).val() == columnObj1.valueEx) {
                            $("#CPForm_13183 option[value='" + $(this).val() + "']").attr("selected", "selected");
                            return false;
                        }
                    });
                }

            }
        }
    });
}

//保存前方法
function SaveBegin() {
    debugger
    var CPForm_11868 = $("#CPForm_11868");//车牌号
    if (CPForm_11868.find("option:selected").val() === "0") {
        CPFormGlobal_FormObj.Data.BD_CarBorrowInfo_CarNumber = "";
        CPFormGlobal_FormObj.Data.BD_CarBorrowInfo.CarNumber = "";
        CPFormGlobal_FormObj.Data.BD_CarBorrowInfo_CarId = "";
        CPFormGlobal_FormObj.Data.BD_CarBorrowInfo.CarId = "";
    } else {
        CPFormGlobal_FormObj.Data.BD_CarBorrowInfo_CarId = CPForm_11868.find("option:selected").val();
        CPFormGlobal_FormObj.Data.BD_CarBorrowInfo.CarId = CPForm_11868.find("option:selected").val();
        CPFormGlobal_FormObj.Data.BD_CarBorrowInfo_CarNumber = CPForm_11868.find("option:selected").text();
        CPFormGlobal_FormObj.Data.BD_CarBorrowInfo.CarNumber = CPForm_11868.find("option:selected").text();
    }

    var CPForm_11870 = $("#CPForm_11870");//借出基地名称
    if (CPForm_11870.find("option:selected").val() === "0") {
        CPFormGlobal_FormObj.Data.BD_CarBorrowInfo_BaseName = "";
        CPFormGlobal_FormObj.Data.BD_CarBorrowInfo.BaseName = "";
    } else {
        var text = CPForm_11870.find("option:selected").text();
        var strsplit = text.split("--");
        CPFormGlobal_FormObj.Data.BD_CarBorrowInfo_BaseName = strsplit[1];
        CPFormGlobal_FormObj.Data.BD_CarBorrowInfo.BaseName = strsplit[1];
    }

    var CPForm_11872 = $("#CPForm_11872");//借入基地名称
    if (CPForm_11872.find("option:selected").val() === "0") {
        CPFormGlobal_FormObj.Data.BD_CarBorrowInfo_BorrowBaseName = "";
        CPFormGlobal_FormObj.Data.BD_CarBorrowInfo.BorrowBaseName = "";
    } else {
        var str = CPForm_11872.find("option:selected").text();
        var ssplit = str.split("--");
        CPFormGlobal_FormObj.Data.BD_CarBorrowInfo_BorrowBaseName = ssplit[1];
        CPFormGlobal_FormObj.Data.BD_CarBorrowInfo.BorrowBaseName = ssplit[1];
    }

    var CPForm_13181 = $("#CPForm_13181");//主司机
    if (CPForm_13181.find("option:selected").val() === "0") {
        CPFormGlobal_FormObj.Data.BD_CarBorrowInfo_MainDriverId = "";
        CPFormGlobal_FormObj.Data.BD_CarBorrowInfo.MainDriverId = "";
        CPFormGlobal_FormObj.Data.BD_CarBorrowInfo_MainDriverName= "";
        CPFormGlobal_FormObj.Data.BD_CarBorrowInfo.MainDriverName = "";
    } else {
        var CPForm_13181text = CPForm_13181.find("option:selected").text();
        var textsplit = CPForm_13181text.split('-');
        CPFormGlobal_FormObj.Data.BD_CarBorrowInfo_MainDriverId = CPForm_13181.find("option:selected").val();
        CPFormGlobal_FormObj.Data.BD_CarBorrowInfo.MainDriverId = CPForm_13181.find("option:selected").val();
        CPFormGlobal_FormObj.Data.BD_CarBorrowInfo_MainDriverName = textsplit[1];
        CPFormGlobal_FormObj.Data.BD_CarBorrowInfo.MainDriverName = textsplit[1];
    }

    var CPForm_13183 = $("#CPForm_13183");//辅司机
    if (CPForm_13183.find("option:selected").val() === "0") {
        CPFormGlobal_FormObj.Data.BD_CarBorrowInfo_AssistantDriverId = "";
        CPFormGlobal_FormObj.Data.BD_CarBorrowInfo.AssistantDriverId = "";
        CPFormGlobal_FormObj.Data.BD_CarBorrowInfo_AssistantDriverName = "";
        CPFormGlobal_FormObj.Data.BD_CarBorrowInfo.AssistantDriverName = "";
    } else {
        var CPForm_13183text = CPForm_13183.find("option:selected").text();
        var tsplit = CPForm_13183text.split('-');
        CPFormGlobal_FormObj.Data.BD_CarBorrowInfo_AssistantDriverId = CPForm_13183.find("option:selected").val();
        CPFormGlobal_FormObj.Data.BD_CarBorrowInfo.AssistantDriverId = CPForm_13183.find("option:selected").val();
        CPFormGlobal_FormObj.Data.BD_CarBorrowInfo_AssistantDriverName  = tsplit[1];
        CPFormGlobal_FormObj.Data.BD_CarBorrowInfo.AssistantDriverName  = tsplit[1];
    }

}

String.prototype.format = function () {

    if (arguments.length == 0) return this;
    for (var s = this, i = 0; i < arguments.length; i++)
        s = s.replace(new RegExp("\\{" + i + "\\}", "g"), arguments[i]);
    return s;

};

function trim(str) { //删除左右两端的空格
    return str.replace(/(^s*)|(s*$)/g, "");
}


//表单加载完成后
function loadDom() {
    debugger
    CPFormGlobal_FormObj.Data.BD_CarBorrowInfo_CarId.splice(0, 2);
    CPFormGlobal_FormObj.Data.BD_CarBorrowInfo_MainDriverId.splice(0, 2);
    CPFormGlobal_FormObj.Data.BD_CarBorrowInfo_AssistantDriverId.splice(0, 2);
    //绑定数据源
    var selectSource = new Object();
    selectSource.FieldEnumDataIns = "CPYKWL";
    selectSource.FieldEnumDataSource = "select CarNumber [text] ,Id  value from BD_CarInfo where  BaseId='{0}' order by Id";
    //绑定基地信息
    var url = "/api/HatchRoom/GetSourceInfo";
    var CPForm_11870Conent = CPFormGlobal_FormObj.Data.BD_CarBorrowInfo.BaseId;
    var select = new Object();
    select.FieldEnumDataIns = selectSource.FieldEnumDataIns;
    select.FieldEnumDataSource = selectSource.FieldEnumDataSource.format(CPForm_11870Conent);
    $.ajax({
        type: "POST",
        url: url,
        async: false,
        data: JSON.stringify(select),
        contentType: 'application/json',
        success: function (data) {
            if (data.length !== 0) {
                debugger;
                //json字符串转化为json对象
                data = eval('(' + data + ')');
                data = eval('(' + data.Column + ')');
                var caridobj = new Object();
                caridobj.textEx = trim("请选择车牌号");
                caridobj.valueEx = trim("0");
                caridobj.listRelateEx = "";
                CPFormGlobal_FormObj.Data.BD_CarBorrowInfo_CarId.push(caridobj);
                for (var i = 0; i < data.length; i++) {
                    var columnObj = data[i];
                    caridobj = new Object();
                    caridobj.textEx = trim(columnObj.textEx);
                    caridobj.valueEx = trim(columnObj.valueEx .toString());
                    caridobj.listRelateEx = "";
                    CPFormGlobal_FormObj.Data.BD_CarBorrowInfo_CarId.push(caridobj);
                }
            }
        }
    });


    //设置主辅司机


    var driverSource = new Object();
    driverSource.FieldEnumDataIns = "CPOrganIns";
    driverSource.FieldEnumDataSource = "select  LoginName+'-'+UserName  as text, a.UserId as value from dbo.CP_User a inner join dbo.CP_RoleUser b on  a.UserId=b.UserId ";
    driverSource.FieldEnumDataSource += "INNER JOIN dbo.CP_DepUser d on a.UserId = d.UserId where b.RoleId = 1013  AND DepId in (select DepId from dbo.CP_Dep where ParentId in ({0}) and DepType = 5) ";



    var CPForm_11870Content = CPFormGlobal_FormObj.Data.BD_CarBorrowInfo.BaseId;//借出基地
    var CPForm_11872Content = CPFormGlobal_FormObj.Data.BD_CarBorrowInfo.BorrowBaseId;//借入基地
    var values = "";
    if (CPForm_11870Content !== "") values = CPForm_11870Content+ ",";
    if (CPForm_11872Content !== "") values += CPForm_11872Content;
    values = (values.substring(values.length - 1) == ',') ? values.substring(0, values.length - 1) : values;
    var driver = new Object();
    driver.FieldEnumDataIns = driverSource.FieldEnumDataIns;
    driver.FieldEnumDataSource = driverSource.FieldEnumDataSource.format(values);
    $.ajax({
        type: "POST",
        url: url,
        async: false,
        data: JSON.stringify(driver),
        contentType: 'application/json',
        success: function (data) {
            if (data.length !== 0) {
                debugger;               
                //json字符串转化为json对象
                data = eval('(' + data + ')');
                data = eval('(' + data.Column + ')');
                var driverobj = new Object();
                driverobj.textEx = trim("请选择主司机");
                driverobj.valueEx = trim("0");
                driverobj.listRelateEx = "";
                CPFormGlobal_FormObj.Data.BD_CarBorrowInfo_MainDriverId.push(driverobj);
                var assistdriverobj = new Object();
                assistdriverobj.textEx = trim("请选择辅司机");
                assistdriverobj.valueEx = trim("0");
                assistdriverobj.listRelateEx = "";
                CPFormGlobal_FormObj.Data.BD_CarBorrowInfo_AssistantDriverId.push(assistdriverobj);
                for (var i = 0; i < data.length; i++) {
                    var columnObj = data[i];
                    driverobj = new Object();
                    driverobj.textEx = trim(columnObj.textEx);
                    driverobj.valueEx = trim(columnObj.valueEx.toString());
                    driverobj.listRelateEx = "";
                    CPFormGlobal_FormObj.Data.BD_CarBorrowInfo_MainDriverId.push(driverobj);
                    assistdriverobj = new Object();
                    assistdriverobj.textEx = trim(columnObj.textEx);
                    assistdriverobj.valueEx = trim(columnObj.valueEx.toString());
                    assistdriverobj.listRelateEx = "";
                    CPFormGlobal_FormObj.Data.BD_CarBorrowInfo_AssistantDriverId.push(assistdriverobj);
                }
            }
        }
    });

}